package com.lingx.web.api.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.lingx.service.LingxService;
import com.lingx.service.UserService;
import com.lingx.web.api.IApi;
@Component
public class Api9027 extends AbstractDevApi implements IApi{
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private LingxService lingxService;
	@Resource
	private UserService userService;
	@Resource
	private ApplicationContext spring;
	@Override
	public int getApiCode() {
		return 9027;
	}
	@Override
	public String getApiName() {
		return "接口授权";
	}
	@Override
	public Map<String, Object> api(Map<String, Object> params) {
		Map<String,Object> ret=IApi.getRetMap(1, "操作成功");
		String roleid=IApi.getParamString(params, "role_id", "");
		String menuid=IApi.getParamString(params, "menu_id", "");
		String checked=IApi.getParamString(params, "checked", "");
		String checkedSub=IApi.getParamString(params, "checkedSub", "");
		//if("true".equals(checkedSub))checkedSub=String.valueOf(this.lingxService.queryForInt("select count(*) from tlingx_menu where fid=?", menuid)>0);
		if("true".equals(checked)&&"false".equals(checkedSub)) {//||"true".equals(checkedSub)
			if(lingxService.queryForInt("select count(*) from tlingx_role_api where role_id=? and apicode=?", roleid,menuid)==0)
			this.jdbcTemplate.update("insert into tlingx_role_api(id,role_id,apicode)values(uuid(),?,?)",roleid,menuid);
			addSubAuth(menuid,roleid);
			
		}else if("false".equals(checked)&&"true".equals(checkedSub)) {//||"true".equals(checkedSub)
			if(lingxService.queryForInt("select count(*) from tlingx_role_api where role_id=? and apicode=?", roleid,menuid)==0) {
			this.jdbcTemplate.update("insert into tlingx_role_api(id,role_id,apicode)values(uuid(),?,?)",roleid,menuid);
			//addSubAuth(menuid,roleid);//该功能不能开启，不叶子项时，变成全部选中
			}
		}else if("false".equals(checked)&&"false".equals(checkedSub)) {
			if(lingxService.queryForInt("select count(*) from tlingx_role_api where role_id=? and apicode=?", roleid,menuid)>0) {
			this.jdbcTemplate.update("delete from tlingx_role_api where role_id=? and apicode=?",roleid,menuid);
			delSubAuth(menuid,roleid);
			}
		}
		this.userService.clearAuthCache();
		return ret;
	}
	
	private void addSubAuth(Object fid,String roleid) {
		List<Map<String,Object>> list=getGroupList(fid.toString());
		for(Map<String,Object> map:list) {
			if(lingxService.queryForInt("select count(*) from tlingx_role_api where role_id=? and apicode=?", roleid,map.get("id"))==0)
				this.jdbcTemplate.update("insert into tlingx_role_api(id,role_id,apicode)values(uuid(),?,?)",roleid,map.get("id"));
			addSubAuth(map.get("id"),roleid);
		}
	}

	private void delSubAuth(Object fid,String roleid) {
		List<Map<String,Object>> list=getGroupList(fid.toString());
		for(Map<String,Object> map:list) {
			this.jdbcTemplate.update("delete from tlingx_role_api where role_id=? and apicode=?",roleid,map.get("id"));
			delSubAuth(map.get("id"),roleid);
		}
	}
	

	private List<Map<String,Object>> getGroupList(String fid){

		Map<String,IApi> listApi=this.spring.getBeansOfType(IApi.class);
		List<Map<String,Object>> list=new ArrayList<>();
		Map<String,Object> map;
		if("0".equals(fid)) {
		Set<String> sets=new HashSet<>();
		for(IApi api:listApi.values()) {
			sets.add(api.getGroupName());
		}
		for(String str:sets) {
			map=new HashMap<>();
			map.put("text", str);
			map.put("id", str);
			list.add(map);
		}
		
		}else {
			for(IApi api:listApi.values()) {
				if(fid.equals(api.getGroupName())) {
					map=new HashMap<>();
					map.put("text", api.getApiCode()+" - "+api.getApiName());
					map.put("id", api.getApiCode());
					list.add(map);
				}
			}
		}
		
		Collections.sort(list, new Comparator<Map<String,Object>>() {
			@Override
			public int compare(Map<String,Object> o1, Map<String,Object> o2) {
				Map<String,Object> m1=o1;
				Map<String,Object> m2=o2;
				return (m1.get("id").toString().compareTo(m2.get("id").toString())) ;
			}});
		return list;
	}

}
